package com.ciwong.epaper.modules.epaper.util;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lzr on 2016/11/14.
 */
public class ViewPageTitle extends LinearLayout {

    public String Tag = "ViewPageTitle";

    private ImageView cursorView;
    private List<View> mViews;
    private List<String> titles;
    private LinearLayout mainLayout;
    private LayoutInflater infalter;
    private ViewPager mViewPager;
    private Context mContext;
    private int mainWidthPixels;
    //每一项的宽度
    private int itemPixels;

    private ViewPageChanag viewPageChanag;

    private int pageCheck;
    private int tGravity = -1;
    private int tPadding = -1;
    private int left;
    private int top;
    private int right;
    private int bottom;


    public void setViewPageChanag(ViewPageChanag viewPageChanag) {
        this.viewPageChanag = viewPageChanag;
    }

    public ViewPageTitle(Context context) {
        super(context);
        init(context);
    }

    public int getPageCheck() {
        return pageCheck;
    }

    public ViewPageTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ViewPageTitle(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        mainWidthPixels = getWindowsWidth((Activity) context);
        mViews = new ArrayList<View>();
        infalter = LayoutInflater.from(mContext);
    }


    public void setTitles(List<String> titles) {
        this.titles = titles;
        if (titles != null)
            itemPixels = mainWidthPixels / titles.size();
    }

    public void setmViewPager(ViewPager mViewPager) {
        this.mViewPager = mViewPager;
    }

    public void loadView() {
        deBug("----loadView------" + itemPixels);
        View view = infalter.inflate(R.layout.view_page_titles, this);
        mainLayout = (LinearLayout) view.findViewById(R.id.title_main);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /**
             *
             * @param position  当前页面，及你点击滑动的页面
             * @param positionOffset 当前页面偏移的百分比
             * @param positionOffsetPixels 当前页面偏移的像素位置
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            /**
             * 此方法是页面跳转完后得到调用，arg0是你当前选中的页面的Position（位置编号）
             * @param position
             */
            @Override
            public void onPageSelected(int position) {
                pageCheck = position;
                if (viewPageChanag != null) {
                    viewPageChanag.onPageSelected(position);
                }
                Log.d("xixin", "---------滑动onPageSelected----------------");
//                mScrollView.setScro(false);
                for (int i = 0; i < mViews.size(); i++) {
                    if (position == i) {
                        ((TextView) mViews.get(i)).setTextColor(mContext.getResources().getColor(R.color.color_light_green));
                    } else {
                        ((TextView) mViews.get(i)).setTextColor(mContext.getResources().getColor(R.color.bookcase_dialog_bg));
                    }
                }
//                ObjectAnimator moveIn;
                if (position == 1) {
                    cursorView.setImageResource(R.mipmap.right_scr);
//                     moveIn = ObjectAnimator.ofFloat(cursorView, "translationX", cursorView.getTranslationX(), cursorView.getWidth());
                } else {
                    cursorView.setImageResource(R.mipmap.left_scr);
//                    moveIn = ObjectAnimator.ofFloat(cursorView, "translationX", cursorView.getTranslationX(), 0);
                }
//                moveIn.setDuration(200);
//                moveIn.start();
            }

            /**
             * 三种状态（0，1，2）。arg0 ==1的时辰默示正在滑动，arg0==2的时辰默示滑动完毕了，arg0==0的时辰默示什么都没做。
             * @param state
             */
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //layout添加子项
        for (int i = 0; i < titles.size(); i++) {
            final int index = i;
            View tempView = infalter.inflate(R.layout.view_page_item_title, null);
            TextView title = (TextView) tempView.findViewById(R.id.item_title);
            if (tGravity != -1) {
                ((RelativeLayout) title.getParent()).setGravity(tGravity);
            }
            title.setPadding(left, top, right, bottom);
            title.setText(titles.get(i));
            tempView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mViewPager != null) {
                        mViewPager.setCurrentItem(index);
                    }
                }
            });
            if (i == 0) {
                title.setTextColor(mContext.getResources().getColor(R.color.color_light_green));
            }
            mViews.add(title);
            mainLayout.addView(tempView, new ViewGroup.LayoutParams(itemPixels, ViewGroup.LayoutParams.MATCH_PARENT));
        }
        cursorView = (ImageView) view.findViewById(R.id.video_cursor_view);
    }

    /**
     * 设置tittle text的位置
     * @param gravity
     */

    public void setTittleGravity(int gravity) {
        this.tGravity = gravity;
    }

    /**
     * 设置title text 的padding
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setTittlePadding_dp(int left, int top, int right, int bottom) {
        this.left = dp2px(getContext(),left);
        this.top =  dp2px(getContext(),top);
        this.right = dp2px(getContext(), right);
        this.bottom =  dp2px(getContext(),bottom);
    }

    /**
     * convert dp to its equivalent px
     * <p>
     * 将dp转换为与之相等的px
     */
    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 获取屏幕的宽度  这个可以作为参数传入本类中 更灵活一些
     *
     * @param activity
     * @return
     */
    public int getWindowsWidth(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }


    private void deBug(String mes) {
        Log.d(Tag, mes);
    }


    public interface ViewPageChanag {
        public void onPageSelected(int position);
    }

}
